package com.sgu6.sgu6_machine.ui.activity;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseA;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.dto.UpdateMachinePwdDto;

import com.sgu6.sgu6_machine.presenter.MachineP;
import com.sgu6.sgu6_machine.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeMachinePwdA extends BaseA {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_machine_no)
    TextView tvMachineNo;
    @BindView(R.id.et_machine_pwd)
    EditText etMachinePwd;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    String machineNo = "000-0000000", machinePwd;

    AlertDialog.Builder dialog;
    MachineP machineP;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_machine_pwd);
        ButterKnife.bind(this);

        initUI();

        initParams();

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    public void initUI() {
        initToolbar();
        initMachineNo();
        initDialog();
    }

    private void initParams() {
        machineP = new MachineP();
    }

    private void initMachineNo() {
        tvMachineNo.setText(MachineContainer.machineConfigDto.machineNo);
    }


    private void initDialog() {
        dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示");
        dialog.setMessage("是否修改机器密码？");
        dialog.setNegativeButton("取消", (dialog, which) -> {
        });
        dialog.setPositiveButton("确定", (dialog, which) -> {
            machineP.updatePwd(MachineContainer.machineConfigDto.machineNo, machinePwd);
            progressBar.setVisibility(View.VISIBLE);
        });
    }

    private void initToolbar() {
        toolbar.setTitle("修改机器密码");
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setBackgroundResource(R.color.colorPrimaryDark);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdatePwd(UpdateMachinePwdDto dto) {
        progressBar.setVisibility(View.GONE);
        Log.i(TAG, "onUpdatePwd: " + dto.isSucceed());
        if (dto.isSucceed()) {
            ToastUtil.show(ChangeMachinePwdA.this, "机器密码修改成功");
            try {
//                MachineConfig.machineConfigDto.machineNo = machineNo;
                MachineContainer.machineConfigDto.machinePassword = machinePwd;
                machineConfig.updateMachineConfig(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.finish();
        } else {
            ToastUtil.show(ChangeMachinePwdA.this, dto.getErrMsg());
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        machinePwd = etMachinePwd.getText().toString().trim();
        if (machineNo.length() > 0 && machinePwd.length() > 0) {
            dialog.show();
        } else {
            ToastUtil.show(this, "密码不能为空");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
